package com.cgy.wodeyigui.product.service.impl;

import com.cgy.common.to.SkuReductionTo;
import com.cgy.common.to.SpuBoundTo;
import com.cgy.common.utils.R;
import com.cgy.wodeyigui.product.entity.*;
import com.cgy.wodeyigui.product.feign.SpuFeignService;
import com.cgy.wodeyigui.product.service.*;
import com.cgy.wodeyigui.product.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cgy.common.utils.PageUtils;
import com.cgy.common.utils.Query;

import com.cgy.wodeyigui.product.dao.SkuInfoDao;
import org.springframework.transaction.annotation.Transactional;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    AttrService attrService;
    @Autowired
    SpuImagesService imagesService;
    @Autowired
    ProductAttrValueService productAttrValueService;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    SpuFeignService spuFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void saveSpuInfo(SpuSaveVo vo) {
        //1:保存spu基本信息
        SpuInfoEntity infoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo,infoEntity);
        infoEntity.setCreateTime(new Date());
        infoEntity.setUpdateTime(new Date());
        this.saveBaseSpuInfo(infoEntity);
        //2:保存spu的描述图片
        List<String> decript = vo.getDecript();
        SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
        descEntity.setSpuId(infoEntity.getId());
        descEntity.setDecript(String.join(",",decript));
        spuInfoDescService.saveSpuInfoDesc(descEntity);

        //3：保存spu的图片集
        List<String> images = vo.getImages();
        imagesService.saveImage(infoEntity.getId(),images);
        //4：保存spu的规格参数
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            productAttrValueEntity.setAttrId(attr.getAttrId());
            AttrEntity byId = attrService.getById(attr.getAttrId());
            productAttrValueEntity.setAttrName(byId.getAttrName());
            productAttrValueEntity.setAttrValue(attr.getAttrValues());
            productAttrValueEntity.setQuickShow(attr.getShowDesc());
            productAttrValueEntity.setSpuId(infoEntity.getId());
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveProductAttr(collect);
        //保存当前spu的对丁的所有sku信息
            //1)sku的基本信息
        List<Skus> skus = vo.getSkus();
        if(skus!=null && skus.size()>0){
            skus.forEach(item->{
                String defaultImg = "";

                for (Images image : item.getImages()){
                    if(image.getDefaultImg()==1){
                        defaultImg = image.getImgUrl();
                    }
                }
                SkuInfoEntity skuInfo = new SkuInfoEntity();
                BeanUtils.copyProperties(item,skuInfo);
                skuInfo.setBrandId(infoEntity.getBrandId());
                skuInfo.setCatalogId(infoEntity.getCatalogId());
                skuInfo.setSaleCount(0L);
                skuInfo.setSpuId(infoEntity.getId());
                skuInfo.setSkuDefaultImg(defaultImg);
                skuInfoService.saveSkuinfo(skuInfo);
                Long skuId = skuInfo.getSkuId();


                List<SkuImagesEntity> imagesEntities = item.getImages().stream().map(img -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    skuImagesEntity.setDefaultImg(img.getDefaultImg());
                    return skuImagesEntity;
                }).collect(Collectors.toList());
                //2)当前sku的图片信息
                skuImagesService.saveBatch(imagesEntities);
                //3)销售属性信息
                List<Attr> attr = item.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attr.stream().map(a -> {
                    SkuSaleAttrValueEntity attrValueEntity = new SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(a, attrValueEntity);
                    attrValueEntity.setSkuId(skuId);
                    return attrValueEntity;

                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
                //sku优惠，满减等信息
                SkuReductionTo skureductionTo = new SkuReductionTo();
                BeanUtils.copyProperties(item,skureductionTo);
                skureductionTo.setSkuId(skuId);
                R r1 = spuFeignService.saveSkuReduction(skureductionTo);
                if(r1.getCode()!=0){
                    log.error("远程保存sku优惠信息失败");
                }
                //保存商品积分
                Bounds bounds = vo.getBounds();
                SpuBoundTo spuBoundTo = new SpuBoundTo();
                BeanUtils.copyProperties(bounds,spuBoundTo);
                spuBoundTo.setSpuId(infoEntity.getId());
                R r = spuFeignService.savaSpuBounds(spuBoundTo);
                if(r.getCode()!=0){
                    log.error("远程保存spu积分信息失败");
                }

            });

        }
    }

    @Override
    public void saveBaseSpuInfo(SpuInfoEntity infoEntity) {
        this.baseMapper.insert(infoEntity);
    }

    @Override
    public void saveSkuinfo(SkuInfoEntity skuInfo) {
        this.baseMapper.insert(skuInfo);
    }


}